package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.JacksonUtil;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/admin/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private DtsRoleService dtsRoleService;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private DtsPermissionService dtsPermissionService;

    /**
     * #####  管理员列表角色选项
     *
     * - 接口地址 : http://localhost:8083/admin/role/options
     * - GET请求
     * - 下行数据 :
     *
     *
     *  [
     *                {
     * 			"value": 1,
     * 			"label": "超级管理员"
     *        },
     *        {
     * 			"value": 2,
     * 			"label": "商场管理员"
     *        },
     *        {
     * 			"value": 3,
     * 			"label": "推广管理员"
     *        },
     *        {
     * 			"value": 4,
     * 			"label": "品牌制造商"
     *        }
     * 	],
     */
    @GetMapping("/options")
    public Object options(){
        return ResponseUtil.ok(dtsRoleService.findCatVoList());
    }

    /**
     * http://localhost:8080/admin/role/list
     * ?page=1&limit=20&sort=add_time&order=desc&rolename=%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98
     */
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")
    @RequiresPermissions(value = {"admin:role:list"})
    @GetMapping("/list")
    public Object roleList(Integer page,Integer limit,String sort,String order,String rolename){
        IPage<DtsRole> ipage = dtsRoleService.findPage(page,limit,sort,order,rolename);
        Map data = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     *
     *  角色管理之授权列表
     * - 接口地址 : http://localhost:8083/admin/role/permissions?roleId=5
     *
     * - 下行数据 :
     */
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        Map data = new HashMap();
        String basicPackage = "com.qf";
        List<Permission> permissions = PermissionUtil.listPermission(applicationContext,basicPackage);
        System.out.println(permissions);
        //1:查询当前角色所拥有的所有权限
        Set<String> permSet =  dtsPermissionService.findDtsPermsPermissionByRoleId(roleId);
        List<PermVo> permVoList = PermissionUtil.listPermVo(permissions);
        //2:当前系统中暂时拥有的所有权限
        List<String> assignedPermissions = permissions.stream().map(permission -> {
            return permission.getRequiresPermissions().value()[0];
        }).collect(Collectors.toList());
        if(permSet.contains("*")){
            //当前角色拥有的权限
            data.put("assignedPermissions",assignedPermissions);
        }else{
            //2:判断当前角色拥有的所有权限在总权限中是否存在
            Set<String> permRoleId = permSet.stream().filter(perm -> {
                if (assignedPermissions.contains(perm)) {
                    return true;//有 留下
                } else {
                    return false;//没有 去掉
                }
            }).collect(Collectors.toSet());
            //当前角色拥有的权限
            data.put("assignedPermissions",permRoleId);
        }
        data.put("systemPermissions",permVoList);//显示二级菜单 及功能
        return ResponseUtil.ok(data);

    }
    /**
     * 授权##### 角色授权操作
     *
     * - 接口地址 :  http://localhost:8083/admin/role/permissions
     * - 上行数据:
     * {
     * 	"permissions": [
     * 		"admin:category:update",
     * 		"admin:category:list",
     * 		"admin:category:read",
     * 		"admin:category:delete",
     * 		"admin:category:create",
     * 		"admin:role:permission:update",
     * 		"admin:role:update",
     * 		"admin:role:list",
     * 		"admin:role:read",
     * 		"admin:role:delete",
     * 		"admin:role:permission:get",
     * 		"admin:role:create"
     * 	],
     * 	"roleId": 31
     * }
     */
    @PostMapping("/permissions")
    public Object permissions(@RequestBody String body){

        List<String> permissions = JacksonUtil.parseStringList(body, "permissions");
        Integer roleId = JacksonUtil.parseInteger(body, "roleId");
        dtsPermissionService.updatePermissionsByRoleId(permissions,roleId);

        //修改
        return ResponseUtil.ok();

    }
}
